package com.woniu.service.mallSystem.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniu.mapper.mallSystem.CategoryMapper;
import com.woniu.model.mallSystem.dto.CategoryTypeDto;
import com.woniu.model.mallSystem.entity.Category;
import com.woniu.model.mallSystem.vo.CategoryVo;
import com.woniu.service.mallSystem.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 品类表 服务实现类
 * </p>
 *
 * @author Lzj
 * @since 2023-10-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    /**
     * 查询所有品类
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ICategoryService categoryService;
    @Override
    public PageInfo<Category> findCategories(Integer pageNum, Integer pageSize, Map<String,Object> condition) {
        // 参数1：分页对象
        PageHelper.startPage(pageNum, pageSize);
        //判断redis是否存在key,没有就查询数据库
        List<Category> categoryList = categoryMapper.queryCategoryByCondition(condition);
        return new PageInfo<>(categoryList);
    }

    /**
     * 根据id查询子品类
     */
    @Override
    public List<Category> getAllChildrenCategory(Integer id) {
        return baseMapper.selectList(Wrappers.lambdaQuery(Category.class)
                .eq(Objects.nonNull(id),Category::getParentCategoryId,id));
    }

    /**
     *
     * 添加品类
     */
    @Transactional
    @Override
    public void addEntity(CategoryTypeDto categoryTypeDto) {

        // 不存在才添加
        Category category = BeanUtil.copyProperties(categoryTypeDto, Category.class);
        category.setParentCategoryId(-1L);
        category.setStatus(1);
        category.setSequence(1);
        category.setCreateTime(new Date());
        categoryMapper.insert(category);

        for (int i = 0; i < categoryTypeDto.getSubTypes().size(); i++) {
            if (categoryTypeDto.getSubTypes().get(i).equals("")
                    || Objects.isNull(categoryTypeDto.getSubTypes().get(i))) continue;
            Category subType = new Category();
            subType.setCategoryName(categoryTypeDto.getSubTypes().get(i));
            subType.setParentCategoryId(category.getId());
            subType.setSequence(1);
            subType.setStatus(1);
            subType.setCreateTime(new Date());
            categoryMapper.insert(subType);
        }
    }

    /**
     * 更新品类
     */
    @Override
    @Transactional
    public void updateCategory(CategoryTypeDto categoryTypeDto) {

        categoryMapper.deleteSubType(categoryTypeDto.getId());

        Category type = new Category();
        BeanUtil.copyProperties(categoryTypeDto, type);
        categoryMapper.updateById(type);

        List<String> types = categoryTypeDto.getSubTypes();
        types.forEach(e -> {
            Category goodsType = new Category();
            goodsType.setParentCategoryId(categoryTypeDto.getId());
            goodsType.setCategoryName(e);
            categoryMapper.insert(goodsType);
        });
    }

    @Override
    public PageInfo<CategoryVo> findAllType(int pageNum, int pageSize, Map<String, Object> condition) {
        // 参数1：分页对象
        PageHelper.startPage(pageNum, pageSize);
        //判断redis是否存在key,没有就查询数据库
        List<Category> categoryList = categoryMapper.queryCategoryByCondition(condition);
        List<CategoryVo> categoryVos = categoryList.stream().filter(e -> e.getParentCategoryId() == -1).map(e -> {
                    CategoryVo categoryVo = new CategoryVo();
                    // 设置CategoryVo的属性，例如：
                    categoryVo.setId(e.getId());
                    categoryVo.setCategoryName(e.getCategoryName());
                    categoryVo.setParentCategoryId(e.getParentCategoryId());
                    categoryVo.setSequence(e.getSequence());
                    categoryVo.setCreateTime(e.getCreateTime());
                    categoryVo.setDescription(e.getDescription());
                    //再过滤一次找到二级菜单，并放置到一级菜单中去
                    List<CategoryVo> childrenList = categoryList.stream().filter(m -> m.getParentCategoryId() == e.getId()).map(m -> {
                        CategoryVo childVo = new CategoryVo();
                        childVo.setId(m.getId());
                        childVo.setCategoryName(m.getCategoryName());
                        childVo.setParentCategoryId(m.getParentCategoryId());
                        childVo.setSequence(m.getSequence());
                        childVo.setCreateTime(m.getCreateTime());
                        childVo.setDescription(m.getDescription());
                        return childVo;
                    }).collect(Collectors.toList());
                    //设置children
                    categoryVo.setCategoryVoList(childrenList);
                    return categoryVo;
                }).collect(Collectors.toList());

        return new PageInfo<>(categoryVos);
    }
}
